﻿using UnityEngine;
using System.Collections;
using LuaInterface;
using UnityEngine.UI;
using System;

namespace GMBase.Lua
{
    /// <summary>
    /// lua 引擎驱动
    /// </summary>
    public class LuaEngine : MonoBehaviour
    {
        private static LuaEngine _instance;
        private static GameObject _instanceGo;

        public static LuaState lua = null;

        public static LuaEngine me
        {
            get
            {
                if (_instance == null)
                {
                    //_instanceGo = new GameObject("LuaEngine", new Type[] { typeof(LuaEngine) });
                    _instanceGo = GameObject.Find(Game.GameObjectName);
                    var engine = _instanceGo.AddComponent<LuaEngine>();
                    engine.PrepareScript();

                    _instance = engine;
                }
                return _instance;
            }
        }

        public void Init()
        {
            // 目前仅用于初始化单例
        }

        private LuaEngine()
        {
        }

        private void PrepareScript()
        {
            lua = new LuaState();
            LuaBinder.Bind(lua);
            lua.Start();

            string fullPath = Application.dataPath + "/Code/Performance/Lua/";
            lua.AddSearchPath(fullPath);
            lua.Require("Require/require");
        }

        private void OnApplicationQuit()
        {
            lua.Dispose();
            lua = null;
        }
    }
}